﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CRM.Entity;
using CRM.BLL;
using DP.Web.UI;
using DP.Common;
using System.Text;

namespace CRM.Web.VehicleManage
{
    public partial class AddVehicleCustomer : Common.PageBase
    {
        UserLoginInfo _userLoginInfo;
        private T_CUSTOMER_BLL bll = new T_CUSTOMER_BLL();
        private T_DICTIONARY_BLL dBll = new T_DICTIONARY_BLL();
        private T_BUSINESSDIVISION_BLL bdBll = new T_BUSINESSDIVISION_BLL();
        private T_LOCATIONINFORMATION_BLL liBll = new T_LOCATIONINFORMATION_BLL();
        private T_SPECIALLIST_BLL slBll = new T_SPECIALLIST_BLL();
        private T_DEALER_BLL dealerBll = new T_DEALER_BLL();
        private T_CUSTOMERTELINDEX_BLL ctBll = new T_CUSTOMERTELINDEX_BLL();
        private T_VEHICLECUSTOMER_BLL vcBll = new T_VEHICLECUSTOMER_BLL();

        protected override void OnInit(EventArgs e)
        {
            base.VerificationRight = false;
            base.OnInit(e);
            _userLoginInfo = UserLoginInfo.GetLoginInfo();
            PageHelper.SetPrePageUrl(this.Page);
            //PageHelper.SetPopCalender(this.Page, txtCREATETIME);
            //PageHelper.SetPopCalender(this.Page, txtLASTMODIFYTIME);
            //PageHelper.SetPopCalender(this.Page, txtFTIMESTAMP);
            PageHelper.SetPopCalender(this.Page, txtBIRTHDATE);
            PageHelper.SetPopCalender(this.Page, txtBECOMEDATE);

        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                ddlBUSINESSDIVISIONIDBinding();
                ddlBUSINESSDIVISIONID_SelectedIndexChanged(null, null);
                ddlDEALERIDBinding();
                ddlCUSTOMERBASEBinding();
                ddlCUSTOMERTYPEBinding();
                ddlCUSTOMERLEVELBinding();
                ddlSPECIALTYPEBinding();
                ddlSTATUSBinding();
                ddlBIGCUSTOMERTYPEBinding();
                ddlFIRSTCONTACTTYPEBinding();


                pageDataInit();
            }
        }

       

        #region 页面初始化数据绑定

        private void ddlFIRSTCONTACTTYPEBinding()
        {
            ddlFIRSTCONTACTTYPE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("FirstContactType"));
            ddlFIRSTCONTACTTYPE.DataTextField = "DictValue";
            ddlFIRSTCONTACTTYPE.DataValueField = "DictKey";
            ddlFIRSTCONTACTTYPE.DataBind();


        }

        /// <summary>
        /// DDLs the BUSINESSDIVISIONID binding.
        /// </summary>
        private void ddlBUSINESSDIVISIONIDBinding()
        {
            ddlBUSINESSDIVISIONID.DataSource = bdBll.GetCacheList(this.Page,
                                                                  String.Format(" and Status = {0} ",
                                                                                ((int) Status.启用).ToString()), "Status");
            ddlBUSINESSDIVISIONID.DataTextField = "BUSINESSDIVISIONNAME";
            ddlBUSINESSDIVISIONID.DataValueField = "BUSINESSDIVISIONID";
            ddlBUSINESSDIVISIONID.DataBind();

        }


        /// <summary>
        /// DDLs the CUSTOMERBASE binding.
        /// </summary>
        private void ddlCUSTOMERBASEBinding()
        {
            ddlCUSTOMERBASE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CustomerBase"));
            ddlCUSTOMERBASE.DataTextField = "DictValue";
            ddlCUSTOMERBASE.DataValueField = "DictKey";
            ddlCUSTOMERBASE.DataBind();
            ddlCUSTOMERBASE.Items.Insert(0, new ListItem("", ""));

        }

        /// <summary>
        /// DDLs the CUSTOMERTYPE binding.
        /// </summary>
        private void ddlCUSTOMERTYPEBinding()
        {
            ddlCUSTOMERTYPE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CustomerType"));
            ddlCUSTOMERTYPE.DataTextField = "DictValue";
            ddlCUSTOMERTYPE.DataValueField = "DictKey";
            ddlCUSTOMERTYPE.DataBind();
            ddlCUSTOMERTYPE.Items.Insert(0, new ListItem("", ""));

        }

        /// <summary>
        /// DDLs the CUSTOMERLEVEL binding.
        /// </summary>
        private void ddlCUSTOMERLEVELBinding()
        {
            ddlCUSTOMERLEVEL.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("CustomerLevel"));
            ddlCUSTOMERLEVEL.DataTextField = "DictValue";
            ddlCUSTOMERLEVEL.DataValueField = "DictKey";
            ddlCUSTOMERLEVEL.DataBind();
            ddlCUSTOMERLEVEL.Items.Insert(0, new ListItem("", ""));

        }

        /// <summary>
        /// DDLs the BIGCUSTOMERTYPE binding.
        /// </summary>
        private void ddlBIGCUSTOMERTYPEBinding()
        {
            ddlBIGCUSTOMERTYPE.DataSource = dBll.GetCacheListByType(this.Page, DictionaryType.GetNameByKey("BigCustomerType"));
            ddlBIGCUSTOMERTYPE.DataTextField = "DictValue";
            ddlBIGCUSTOMERTYPE.DataValueField = "DictKey";
            ddlBIGCUSTOMERTYPE.DataBind();
            ddlBIGCUSTOMERTYPE.Items.Insert(0, new ListItem("", ""));

        }

        /// <summary>
        /// DDLs the STATUS binding.
        /// </summary>
        private void ddlSTATUSBinding()
        {
            EnumHelper.FillListControl(ddlSTATUS, typeof(CustomerStatus));

        }

        /// <summary>
        /// DDLs the SPECIALTYPE binding.
        /// </summary>
        private void ddlSPECIALTYPEBinding()
        {
            ddlSPECIALTYPE.Items.Clear();
            EnumHelper.FillListControl(ddlSPECIALTYPE, typeof (CustomerSpecialType));
        }


        private void ddlCOUNTRYBinding()
        {

            ddlCOUNTRY.DataSource = liBll.GetCacheListByParentCode(this.Page, "");
            ddlCOUNTRY.DataTextField = "NAME";
            ddlCOUNTRY.DataValueField = "CODE";
            ddlCOUNTRY.DataBind();

            ddlCOUNTRY.Items.Insert(0, new ListItem("", ""));
        }

        private void ddlPROVINCEBinding()
        {
            ddlPROVINCE.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlCOUNTRY.SelectedValue.Trim());
            ddlPROVINCE.DataTextField = "NAME";
            ddlPROVINCE.DataValueField = "CODE";
            ddlPROVINCE.DataBind();
            ddlPROVINCE.Items.Insert(0, new ListItem("", ""));
        }

        private void ddlPREFECTURECITYBinding()
        {
            ddlPREFECTURECITY.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlPROVINCE.SelectedValue.Trim());
            ddlPREFECTURECITY.DataTextField = "NAME";
            ddlPREFECTURECITY.DataValueField = "CODE";
            ddlPREFECTURECITY.DataBind();
            ddlPREFECTURECITY.Items.Insert(0, new ListItem("", ""));
        }

        private void ddlCITYBinding()
        {
            ddlCITY.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlPREFECTURECITY.SelectedValue.Trim());
            ddlCITY.DataTextField = "NAME";
            ddlCITY.DataValueField = "CODE";
            ddlCITY.DataBind();
            ddlCITY.Items.Insert(0, new ListItem("", ""));

        }

        private void ddlVILLAGETOWNBinding()
        {
            //ddlVILLAGETOWN.DataSource = liBll.GetCacheListByParentCode(this.Page, ddlCITY.SelectedValue.Trim());
            //ddlVILLAGETOWN.DataTextField = "NAME";
            //ddlVILLAGETOWN.DataValueField = "CODE";
            //ddlVILLAGETOWN.DataBind();
            //ddlVILLAGETOWN.Items.Insert(0, new ListItem("", ""));
        }





















        /// <summary>
        /// 经销商编号 下拉列表数据绑定
        /// </summary>
        private void ddlDEALERIDBinding()
        {
            //string businessDivisionCode = string.Empty;
            //T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
            //if (bdEntity != null)
            //{
            //    businessDivisionCode = bdEntity.BUSINESSDIVISIONCODE;
            //}
            //ddlDEALERID.DataSource = dealerBll.GetList(string.Format(" and BUSINESSDIVISIONCODE = '{0}' ", businessDivisionCode));
            //ddlDEALERID.DataTextField = "DEALERNAME";
            //ddlDEALERID.DataValueField = "DEALERID";
            //ddlDEALERID.DataBind();
        }






        #endregion

        /// <summary>
        /// Pages the data init.
        /// </summary>
        private void pageDataInit()
        {

            txtCUSTOMERID.Text = bll.GenerateCustomerID(_userLoginInfo);
            txtCUSTOMERCODE.Text = txtCUSTOMERID.Text;
            string phone = PageHelper.GetQueryString(this.Page, "PHONE");
            txtFIRSTCONTACTTEL.Text = phone;
            txtBECOMEDATE.Text = DateTime.Now.ToString("yyyy-MM-dd");
            //if(phone.Length == 11)
            //{
            //    txtMOBILEPHONE.Text = phone;
            //}
            //else
            //{
            //    txtHOMEPHONE.Text = phone;
            //}
            lblTitle.Text = "客户信息添加";

            #region 客户地区级联

            ddlCOUNTRYBinding();
            ddlCOUNTRY_SelectedIndexChanged(null, null);
            ddlPROVINCEBinding();
            ddlPROVINCE_SelectedIndexChanged(null, null);
            ddlPREFECTURECITYBinding();
            ddlPREFECTURECITY_SelectedIndexChanged(null, null);
            ddlCITYBinding();
            ddlCITY_SelectedIndexChanged(null, null);
            ddlVILLAGETOWNBinding();

            #endregion

            txtCREATEPEOPLE.Enabled = false;
            //txtCREATEPEOPLEID.Enabled = false;
            txtCREATETIME.Enabled = false;
            txtLASTMODIFYPEOPLE.Enabled = false;
            //txtLASTMODIFYPEOPLEID.Enabled = false;
            txtLASTMODIFYTIME.Enabled = false;

            txtCUSTOMERID.ReadOnly = true;
            if (!UserLoginInfo.IsAdministrator())
            {
                ddlBUSINESSDIVISIONID.SelectedValue = _userLoginInfo.UserEntity.BUSINESSDIVISIONID.ToString();
                ddlBUSINESSDIVISIONID.Enabled = false;
            }


        }

        /// <summary>
        /// Handles the Click event of the btnOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void btnOK_Click(object sender, EventArgs e)
        {
            T_CUSTOMER_Entity entity = new T_CUSTOMER_Entity();

            PageHelper.GetControlValue<T_CUSTOMER_Entity>(this.Page, ref entity);
            entity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
            entity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
            entity.CREATETIME = DateTime.Now;
            entity.LASTMODIFYPEOPLE = _userLoginInfo.UserEntity.USERNAME;
            entity.LASTMODIFYPEOPLEID = _userLoginInfo.UserEntity.USERID;
            entity.LASTMODIFYTIME = DateTime.Now;
            //事业部
            T_BUSINESSDIVISION_Entity bdEntity = bdBll.GetCache_T_BUSINESSDIVISION_EntityByKey(this.Page, StringHelper.ToInt(ddlBUSINESSDIVISIONID.SelectedValue.Trim()));
            if (bdEntity != null)
            {
                entity.BUSINESSDIVISIONCODE = bdEntity.BUSINESSDIVISIONCODE;
                entity.BUSINESSDIVISIONNAME = bdEntity.BUSINESSDIVISIONNAME;
            }
            //经销商
            //T_DEALER_Entity dealerEntity = dealerBll.GetCache_T_DEALER_EntityByKey(this.Page,
            //                                                                       StringHelper.ToInt(
            //                                                                           ddlDEALERID.SelectedValue.Trim()));
            //if (dealerEntity != null)
            //{
            //    entity.DEALERCODE = dealerEntity.DEALERCODE;
            //    entity.DEALERNAME = dealerEntity.DEALERNAME;
            //}

            try
            {
                long rel = bll.Insert(entity);
                if (rel > 0)
                {
                    ctBll.UpdateCustomerTelIndex(entity);
                    CustomerSpecialType type =
                        (CustomerSpecialType)Enum.Parse(typeof(CustomerSpecialType), ddlSPECIALTYPE.SelectedValue);
                    slBll.UpdateSpecialList(entity, type, _userLoginInfo, entity.HOMEPHONE.Trim());
                    InsertVehicleCustomer(entity);
                    PageHelper.ExecuteJS("RefreshVehiclContactList();");
                    //PageHelper.ShowMessage(this.Page, "添加成功！", "list.aspx");
                }
                else
                {
                    PageHelper.ShowMessage(this.Page, "添加失改！");
                }

            }
            catch (Exception ex)
            {
                
                    PageHelper.ShowMessage(this.Page, "添加失改！" + ex.Message);
            }

        }

        /// <summary>
        /// 添加客户车辆关联关系
        /// Inserts the vehicle customer.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <returns></returns>
        private bool InsertVehicleCustomer(T_CUSTOMER_Entity entity)
        {
            bool rel = false;
            string vinCode = PageHelper.GetQueryString(this.Page, "VINCODE");
            string engineNumber = PageHelper.GetQueryString(this.Page, "ENGINENUMBER");
            string dealerCode = PageHelper.GetQueryString(this.Page, "DEALERCODE");
            if(String.IsNullOrEmpty(vinCode))
            {
                return rel;
            }
            try
            {
                
                T_VEHICLECUSTOMER_Entity vcEntity = new T_VEHICLECUSTOMER_Entity();
                vcEntity.VINCODE = vinCode;
                    vcEntity.BUSINESSDIVISIONCODE = entity.BUSINESSDIVISIONCODE;
                vcEntity.BUSINESSDIVISIONID = entity.BUSINESSDIVISIONID;
                vcEntity.BUSINESSDIVISIONNAME = entity.BUSINESSDIVISIONNAME;
                vcEntity.CREATEPEOPLE = _userLoginInfo.UserEntity.USERNAME;
                vcEntity.CREATEPEOPLEID = _userLoginInfo.UserEntity.USERID;
                vcEntity.CREATETIME = DateTime.Now;
                vcEntity.CUSTOMERCODE = entity.CUSTOMERCODE;
                vcEntity.DEALERCODE = dealerCode;
                vcEntity.ENGINENUMBER = engineNumber;
                vcEntity.IDNUMBER = entity.IDNUMBER;
                vcEntity.CUSTOMERID = entity.CUSTOMERID;
                vcBll.Insert(vcEntity);
            }
            catch (Exception)
            {

            }
            return rel;
        }



        #region DDL  事件

        protected void ddlCOUNTRY_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlPROVINCEBinding();
            ddlPREFECTURECITYBinding();
            ddlCITYBinding();
            ddlVILLAGETOWNBinding();
            txtADDRESS.Text = ddlCOUNTRY.SelectedItem.Text;

        }

        protected void ddlPROVINCE_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlPREFECTURECITYBinding(); 
            ddlCITYBinding();
            ddlVILLAGETOWNBinding();
            txtADDRESS.Text = ddlCOUNTRY.SelectedItem.Text;
            txtADDRESS.Text += ddlPROVINCE.SelectedItem.Text;

        }

        protected void ddlPREFECTURECITY_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlCITYBinding();
            ddlVILLAGETOWNBinding();
            txtADDRESS.Text = ddlCOUNTRY.SelectedItem.Text;
            txtADDRESS.Text += ddlPROVINCE.SelectedItem.Text;
            txtADDRESS.Text += ddlPREFECTURECITY.SelectedItem.Text;
        }

        protected void ddlCITY_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlVILLAGETOWNBinding();
            txtADDRESS.Text = ddlCOUNTRY.SelectedItem.Text;
            txtADDRESS.Text += ddlPROVINCE.SelectedItem.Text;
            txtADDRESS.Text += ddlPREFECTURECITY.SelectedItem.Text;
            txtADDRESS.Text += ddlCITY.SelectedItem.Text;
        }
        protected void ddlBUSINESSDIVISIONID_SelectedIndexChanged(object sender, EventArgs e)
        {
            ddlDEALERIDBinding();
        }

        #endregion


    }
}
